package com.hmall.gateway.config;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
@Order(-1)
public class GatewayFilter implements org.springframework.cloud.gateway.filter.GatewayFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        MultiValueMap<String, String> params = exchange.getRequest().getQueryParams();
//        获取请求头参数
        String authorization = params.getFirst("authorization");
//        校验
        if("2".equals(authorization)){
            return chain.filter(exchange); //放行
        }
//        拦截禁止访问，设置状态码
        exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
//        结束处理
        return exchange.getResponse().setComplete();
    }
}
